package com.fzhucc.modules.subject.service;

import com.fzhucc.base.PageDTO;
import com.fzhucc.modules.subject.domain.dto.SubjectHighfieldsType3CreateDto;
import com.fzhucc.modules.subject.domain.dto.SubjectHighfieldsType3QueryCriteria;
import com.fzhucc.modules.subject.domain.dto.SubjectHighfieldsType3QueryDto;
import com.fzhucc.modules.subject.domain.entity.SubjectHighfieldsType3;
import com.fzhucc.modules.subject.repository.SubjectHighfieldsType3Repository;
import com.fzhucc.modules.subject.service.mapstruct.SubjectHighfieldsType3Mapper;
import com.fzhucc.utils.FileUtil;
import com.fzhucc.utils.QueryHelp;
import com.fzhucc.utils.ValidationUtil;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
* @website https://fzhucc.com
* @description 服务实现
* @author yyg
* @since 2022-09-26 00:00:00
**/
@Service
@RequiredArgsConstructor
public class SubjectHighfieldsType3Service {

    private final SubjectHighfieldsType3Repository subjectHighfieldsType3Repository;
    private final SubjectHighfieldsType3Mapper subjectHighfieldsType3Mapper;

    
    public PageDTO<SubjectHighfieldsType3QueryDto> queryAll(SubjectHighfieldsType3QueryCriteria criteria, Pageable pageable){
        Page<SubjectHighfieldsType3> page = subjectHighfieldsType3Repository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),pageable);
        return PageDTO.by(page.map(subjectHighfieldsType3 -> subjectHighfieldsType3Mapper.toQueryDto(subjectHighfieldsType3)));
    }

    
    public List<SubjectHighfieldsType3QueryDto> queryAll(SubjectHighfieldsType3QueryCriteria criteria){
        List<SubjectHighfieldsType3> subjectHighfieldsType3List = subjectHighfieldsType3Repository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder));
        return subjectHighfieldsType3List.stream().map(subjectHighfieldsType3 -> subjectHighfieldsType3Mapper.toQueryDto(subjectHighfieldsType3)).collect(Collectors.toList());
    }

    
    @Transactional
    public SubjectHighfieldsType3QueryDto findById(Integer id) {
        SubjectHighfieldsType3 subjectHighfieldsType3 = subjectHighfieldsType3Repository.findById(id).orElseGet(SubjectHighfieldsType3::new);
        ValidationUtil.isNull(subjectHighfieldsType3.getId(),"SubjectHighfieldsType3","id",id);
        return subjectHighfieldsType3Mapper.toQueryDto(subjectHighfieldsType3);
    }

    
    @Transactional(rollbackFor = Exception.class)
    public SubjectHighfieldsType3QueryDto create(SubjectHighfieldsType3CreateDto resources) {
        return subjectHighfieldsType3Mapper.toQueryDto(subjectHighfieldsType3Repository.save(subjectHighfieldsType3Mapper.toEntity(resources)));
    }

    
    @Transactional(rollbackFor = Exception.class)
    public void update(SubjectHighfieldsType3CreateDto resources) {
        SubjectHighfieldsType3 subjectHighfieldsType3 = subjectHighfieldsType3Repository.findById(resources.getId()).orElseGet(SubjectHighfieldsType3::new);
        ValidationUtil.isNull( subjectHighfieldsType3.getId(),"SubjectHighfieldsType3","id",resources.getId());
        subjectHighfieldsType3.copy(subjectHighfieldsType3Mapper.toEntity(resources));
        subjectHighfieldsType3Repository.save(subjectHighfieldsType3);
    }

    
    public void deleteAll(Integer[] ids) {
        for (Integer id : ids) {
            subjectHighfieldsType3Repository.deleteById(id);
        }
    }

    
    public void download(List<SubjectHighfieldsType3QueryDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (SubjectHighfieldsType3QueryDto subjectHighfieldsType3 : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("highfields3",  subjectHighfieldsType3.getHighfields3());
            map.put("name",  subjectHighfieldsType3.getName());
            map.put("highfields2",  subjectHighfieldsType3.getHighfields2());
            map.put("sort",  subjectHighfieldsType3.getSort());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
}